package com.cyj.controller;

import com.cyj.service.IPFSService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
public class IPFSController {
    @Autowired
    private IPFSService ipfsService;

    @PostMapping("/upload")
    public String saveFile(@RequestParam("file")MultipartFile file){
        return ipfsService.saveFile(file);
    }
    @GetMapping("file/{hash}")
    public ResponseEntity<byte[]> loadFile(@PathVariable("hasd") String hash){
        HttpHeaders httpHeaders=new HttpHeaders();
        httpHeaders.add("Content-type", MediaType.ALL_VALUE);
        byte[] bytes=ipfsService.loadFile(hash);
        return ResponseEntity.status(HttpStatus.OK).headers(httpHeaders).body(bytes);
    }
}
